package hip.util;

import hip.util.id.IdWorker;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class LogWatchUtil implements Serializable {

    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(LogWatchUtil.class);

    private Map<String, Object> current = null;

    public LogWatchUtil(String tag, boolean pringLog) {
        current = new ConcurrentHashMap<String, Object>();
        long currentTime = System.currentTimeMillis();
        //String currentLogID = String.valueOf(idWorker.nextId())+"[CurrentThread: "+Thread.currentThread().getName()+"]";
        String currentLogID = String.valueOf(new IdWorker(0, 0).nextId());
        current.put("startTime", currentTime);
        current.put("currentTime", currentTime);
        current.put("currentLogID", currentLogID);
        if (pringLog) {
            log.info("LogID: " + currentLogID + ", Tag: " + tag + ", start from[" + currentTime + "]!");
        }
    }

    public LogWatchUtil(String tag, String flag, boolean pringLog) {
        current = new ConcurrentHashMap<String, Object>();
        long currentTime = System.currentTimeMillis();
        //String currentLogID = String.valueOf(new IdWorker(0, 0).nextId())+"[CurrentThread: "+Thread.currentThread().getName()+"-"+flag+"]";
        String currentLogID = String.valueOf(new IdWorker(0, 0).nextId()) + "-[" + flag + "]";
        current.put("startTime", currentTime);
        current.put("currentTime", currentTime);
        current.put("currentLogID", currentLogID);
        if (pringLog) {
            log.info("LogID: " + currentLogID + ", Tag: " + tag + ", start from[" + currentTime + "]!");
        }
    }

    public void lap(String tag, String message, int timeThreshold) {
        if (current == null) {
            log.error("日志监控未初始化[lap-" + tag + "]!");
            return;
        }
        long preTime = (long) current.get("currentTime");
        String currentLogID = current.get("currentLogID").toString();
        long currentTime = System.currentTimeMillis();
        long cost = currentTime - preTime;
        if (cost >= timeThreshold) {
            log.info("LogID: " + currentLogID + ", Tag: " + tag + ", Cost: " + cost + "ms" + (StringUtils.isNotBlank(message) ? (", Message: " + message) : ""));
        }

        current.put("currentTime", currentTime);
    }

    public void stop(String tag, String message, int timeThreshold) {
        if (current == null) {
            log.error("日志监控未初始化[stop-" + tag + "]!");
            return;
        }
        long startTime = (long) current.get("startTime");
        long preTime = (long) current.get("currentTime");
        String currentLogID = current.get("currentLogID").toString();
        long currentTime = System.currentTimeMillis();
        long cost = currentTime - preTime;
        long costTotal = currentTime - startTime;
        if (cost >= timeThreshold) {
            log.info("LogID: " + currentLogID + ", Tag: " + tag + ", Cost: " + cost + "ms" + (StringUtils.isNotBlank(message) ? (", Message: " + message) : ""));
        }

        log.info("LogID: " + currentLogID + ", 总耗时：" + costTotal + "ms!");
    }

}
